var AIBoard = new Array();

var AIEnemyBuff;// 敌人价值
var AIAllyBuff;// 友军价值
var AIEnemyBuffDecay;// 敌人价值衰减
var AIAllyBuffDecay;// 友军价值衰减
var AIAttackEnemyBuff;// 可攻击敌人价值
var AIEnemyAttackBuff;// 受敌人攻击价值
var AIOTKEnemyBuff;// 单轮可击杀敌人价值
var AIBeOTKBuff;// 可能被单轮击杀价值

function AIReflect(chess,skill){
	return false;
};

function AIGo(){
	GUIHideNowAction();
	if(!_GDMoveDone && CSgetState("可移动",_GDNowChess.id)){
		AIprintMap();
		AIMove();
	}
	else{
		if(CSgetState("可攻击",_GDNowChess.id)){
			AIAttack();
		}
		else{
			GUIHideNowAction();
			_GEEventList.push(new GEEvent("结束",_GDNowChess.id));
			GEAskForResponse();
		}
	}
};

function AIMove(){
	var list = new Array();
	list.push(new GDposition(parseInt(_GDNowChess.position.x),parseInt(_GDNowChess.position.y)));
	var value = AIBoard[list[list.length-1].x][list[list.length-1].y];
	var select = list.slice(0);
	var test = 0;
	var over = false;
	while(!over){
		switch(test){
			case 0:{
				if(select[select.length - 1].x < 5 && _board[select[select.length - 1].x+1][select[select.length - 1].y] == null && select.length - 1 < CSgetState("移动力",_GDNowChess.id)){
					select.push(new GDposition(select[select.length - 1].x+1,select[select.length - 1].y));
					if(AIBoard[select[select.length - 1].x][select[select.length - 1].y] > value){
							list = select.slice(0);
							value = AIBoard[select[select.length - 1].x][select[select.length - 1].y];
					}
					else if(AIBoard[select[select.length - 1].x][select[select.length - 1].y] == value){
						if(select.length < list.length){
							list = select.slice(0);
						}
					}
					test = 0;
					break;
				}
			}
			case 1:{
				if(select[select.length - 1].y < 4 && _board[select[select.length - 1].x][select[select.length - 1].y+1] == null && select.length - 1 < CSgetState("移动力",_GDNowChess.id)){
					select.push(new GDposition(select[select.length - 1].x,select[select.length - 1].y+1));
					if(AIBoard[select[select.length - 1].x][select[select.length - 1].y] > value){
						list = select.slice(0);
						value = AIBoard[select[select.length - 1].x][select[select.length - 1].y];
					}
					else if(AIBoard[select[select.length - 1].x][select[select.length - 1].y] == value){
						if(select.length < list.length){
							list = select.slice(0);
						}
					}
					test = 0;
					break;
				}
			}
			case 2:{
				if(select[select.length - 1].x >0 && _board[select[select.length - 1].x-1][select[select.length - 1].y] == null && select.length - 1 < CSgetState("移动力",_GDNowChess.id)){
					select.push(new GDposition(select[select.length - 1].x-1,select[select.length - 1].y));
					if(AIBoard[select[select.length - 1].x][select[select.length - 1].y] > value){
						list = select.slice(0);
						value = AIBoard[select[select.length - 1].x][select[select.length - 1].y];
					}
					else if(AIBoard[select[select.length - 1].x][select[select.length - 1].y] == value){
						if(select.length < list.length){
							list = select.slice(0);
						}
					}
					test = 0;
					break;
				}
			}
			case 3:{
				if(select[select.length - 1].y > 0 && _board[select[select.length - 1].x][select[select.length - 1].y-1] == null && select.length - 1 < CSgetState("移动力",_GDNowChess.id)){
					select.push(new GDposition(select[select.length - 1].x,select[select.length - 1].y-1));
					if(AIBoard[select[select.length - 1].x][select[select.length - 1].y] > value){
						list = select.slice(0);
						value = AIBoard[select[select.length - 1].x][select[select.length - 1].y];
					}
					else if(AIBoard[select[select.length - 1].x][select[select.length - 1].y] == value){
						if(select.length < list.length){
							list = select.slice(0);
						}
					}
					test = 0;
					break;
				}
				over = true;
				for(var i = select.length -1; i >= 0  ; i--){
					if(0 == i){
						break;
					}
					if(!(select[i].x == select[i-1].x) || !(select[i].y == select[i-1].y - 1)){
						over = false;
						if(select[i].x == select[i-1].x + 1){
							test = 1
						}
						if(select[i].x == select[i-1].x){
							test = 2
						}
						if(select[i].x == select[i-1].x - 1){
							test = 3
						}
						select.length = i;
						break;
					 }
				}
				break;
			}
		}
	}
	if(list.length>1){
		for(var j=0;j<list.length-1;j++){
			_GEEventList.push(new GEEvent("移动",_GDNowChess.id+","+list[j].x+","+list[j].y+","+list[j+1].x+","+list[j+1].y));
		}
		_GDMoveDone = true;
		GEAskForResponse();
	}
	else
		AIAttack();
};

function AIFind(id){
	if(id == undefined)
		return null;
	for(var temp in _ai){
		if(_ai[temp].id == id)
			return _ai[temp];
	}
	return null;
}

function AIAttack(){
	var target = null;
	var bossAi = AIFind(_GDNowChess.boss);
	if(bossAi != null){
		for(var temp = 0 ; temp < bossAi.action.length ; temp++){
			if(bossAi.action[temp].at != undefined){
				var aiCounter = _GDNowChess.aiCounter;
				if(undefined != bossAi.action[temp].deviation)
					aiCounter += parseInt(bossAi.action[temp].deviation);
				if(aiCounter != parseInt(bossAi.action[temp].at)){
					continue;
				}
			}
			
			target = ASGetAll();
			target = ASSkillTarget(_GDNowChess.id,bossAi.action[temp].cast,target);
			
			if(bossAi.action[temp].per != undefined){
				var aiCounter = _GDNowChess.aiCounter;
				if(undefined != bossAi.action[temp].deviation)
					aiCounter += parseInt(bossAi.action[temp].deviation);
				if(aiCounter < 0 || aiCounter % parseInt(bossAi.action[temp].per) != 0){
					continue;
				}
			}
			
			if(undefined != bossAi.action[temp].target){
				var requirement =  bossAi.action[temp].target.split(",");
				var param =  bossAi.action[temp].param.split(",");
				for(var i =0 ;i < requirement.length;i++){
					switch(requirement[i]){
						case "生命值小于":{
							target = ASHPLessThan(target,param[i]);
							break;
						}
						case "空格":{
							target = ASBlank(target);
							break;
						}
						case "棋子":{
							target = ASNotBlank(target);
							break;
						}
					}
				}
			}
			if(0 != target.length){
				target = ASRandomOne(target);
				console.log(target.x);
				if(CSgetState("可施放技能"+bossAi.action[temp].cast,_GDNowChess.id)){
					_GEEventList.push(new GEEvent("技能",_GDNowChess.id+","+ bossAi.action[temp].cast +","+target.x+","+target.y));
					_GDAttackDone = true;
					GEAskForResponse();
					return
				}
			}
		}
	}
	if(_GDNowChess.ai == "法师"){
		var spellNo = 0;
		for(var i=0;i<_GDNowChess.skill.length;i++){
			if(AIFindTarget(parseInt(_GDNowChess.skill[i].range),_GDNowChess.skill[i].target,null)!=null && CSgetState("可施放技能"+i,_GDNowChess.id))
				spellNo ++;
		}
		if(spellNo > 0){
			spellNo = parseInt(spellNo * Math.random());
			for(var i=0;i<_GDNowChess.skill.length;i++){
				if(AIFindTarget(parseInt(_GDNowChess.skill[i].range),_GDNowChess.skill[i].target,null)!=null && CSgetState("可施放技能"+i,_GDNowChess.id)){
					if(spellNo == 0){
						target = AIFindTarget(parseInt(_GDNowChess.skill[i].range),_GDNowChess.skill[i].target,null);
						_GEEventList.push(new GEEvent("技能",_GDNowChess.id+","+ i +","+target.x+","+target.y));
						_GDAttackDone = true;
						GEAskForResponse();
						return;
					}
					else{
						spellNo-- ;
					}
				}
			}
		}
	}
	target = null;
	for(var temp in _GDChessList){
		if(_GDChessList[temp].team != _GDNowChess.team && Range(_GDNowChess.position,_GDChessList[temp].position,CSgetState("射程", _GDNowChess.id))){
			if(target == null){
					target = _GDChessList[temp];
				}
			else{
				if(CSgetState("HP", target.id) > CSgetState("HP", _GDChessList[temp].id)){
					target = _GDChessList[temp];
				}
				else if(CSgetState("HP", target.id) == CSgetState("HP", _GDChessList[temp].id)){
					if(CSgetState("攻击力", target.id) > CSgetState("攻击力", _GDChessList[temp].id))
						target = _GDChessList[temp];
				}
			}
		}
	}
	if(target == null){
		_GEEventList.push(new GEEvent("结束",_GDNowChess.id));
		GEAskForResponse();
	}
	else{
		_GEEventList.push(new GEEvent("攻击",_GDNowChess.id+","+target.id));
		_GDAttackDone = true;
		GEAskForResponse();
	}
};

function AIBouns(chess){
	for(var i = 0;i<6;i++){
		for(var j=0;j<5;j++){
			if(_board[i][j] == null || _board[i][j].id == _GDNowChess.id){
				AIBoardBuff(chess,i,j);
			}
		}
	}
};

function AIBoardBuff(chess,x,y){
	var temp;
	if(chess.team == 0){
		temp = AIEnemyBuff - (AIEnemyBuffDecay * SRange(chess.position, new GDposition(x, y)));
		if(temp > 0)
			AIBoard[x][y] += temp;
		if(Range(chess.position, new GDposition(x, y), CSgetState("射程",_GDNowChess.id))){
			AIBoard[x][y] += AIAttackEnemyBuff;
			if(CSgetState("攻击力",_GDNowChess.id)>=CSgetState("HP",chess.id))
				AIBoard[x][y] += AIOTKEnemyBuff;
		}
		if(Range(chess.position, new GDposition(x, y), CSgetState("射程",chess.id))){
			AIBoard[x][y] += AIEnemyAttackBuff;
			if(CSgetState("攻击力",chess.id)>=CSgetState("HP",_GDNowChess.id))
				AIBoard[x][y] += AIBeOTKBuff;
		}
	}
	else if(chess.team == 1 && chess.id != _GDNowChess.id){
		temp = AIAllyBuff - (AIAllyBuffDecay * SRange(chess.position, new GDposition(x, y)));
		if(temp > 0)
			AIBoard[x][y] += temp;
	}
};

function AILuncher(type){
	switch(type){
		case "狂战":{
			AIEnemyBuff = 50;// 敌人价值
			AIAllyBuff = 0;// 友军价值
			AIEnemyBuffDecay = 5;// 敌人价值衰减
			AIAllyBuffDecay = 0;// 友军价值衰减
			AIAttackEnemyBuff = 5;// 可攻击敌人价值
			AIEnemyAttackBuff = 0;// 受敌人攻击价值
			AIOTKEnemyBuff = 99;// 单轮可击杀敌人价值
			AIBeOTKBuff = 0;// 可能被单轮击杀价值
			break;
		}
		case "法师":{
			AIEnemyBuff = 8;// 敌人价值
			AIAllyBuff = 2;// 友军价值
			AIEnemyBuffDecay = 1;// 敌人价值衰减
			AIAllyBuffDecay = 1;// 友军价值衰减
			AIAttackEnemyBuff = 10;// 可攻击敌人价值
			AIEnemyAttackBuff = -3;// 受敌人攻击价值
			AIOTKEnemyBuff = 20;// 单轮可击杀敌人价值
			AIBeOTKBuff = -10;// 可能被单轮击杀价值
			break;
		}
	}
	AICleanBoard();
};

function AICleanBoard(){
	for(var i = 0;i<6;i++){
		AIBoard[i]=new Array();
		for(var j=0;j<5;j++){
			AIBoard[i][j]=0;
		}
	};
	for(var i = 0;i<6;i++){
		for(var j=0;j<5;j++){
			if(_board[i][j] != null){
				AIBouns(_board[i][j]);
			}
		}
	}
};

function AIprintMap(){
	var temp;
	console.log("势能图");
	for(var i = 0;i<5;i++){
		temp = "";
		for(var j=0;j<6;j++){
			temp = temp+"_"+AIBoard[j][i];
		}
		console.log(temp);
	}
};

function AIFindTarget(range,target,remark){
	var counter = 0;
	if(target == "blank"){
		for(var i = 0 ; i<6 ; i++){
			for(var j = 0 ; j<5 ; j++){
				if(_board[i][j]==null&&Range(_GDNowChess.position,new GDposition(i,j),parseInt(range))){
					counter ++;
				}
			}
		}
		if(counter == 0)
			return null;
		var target = parseInt(counter * Math.random());
		counter = 0;
		for(var i = 0 ; i<6 ; i++){
			for(var j = 0 ; j<5 ; j++){
				if(_board[i][j]==null&&Range(_GDNowChess.position,new GDposition(i,j),parseInt(range))){
					if(counter == target)
						return new GDposition(i,j);
					counter ++;
				}
			}
		}
	}
	if(target == "self"){
		return _GDNowChess.position;
	}
	if(target == "target"){
		for(var i = 0 ; i<6 ; i++){
			for(var j = 0 ; j<5 ; j++){
				if(Range(_GDNowChess.position,new GDposition(i,j),parseInt(range))){
					counter ++;
				}
			}
		}
		if(counter == 0)
			return null;
		var target = parseInt(counter * Math.random());
		counter = 0;
		for(var i = 0 ; i<6 ; i++){
			for(var j = 0 ; j<5 ; j++){
				if(Range(_GDNowChess.position,new GDposition(i,j),parseInt(range))){
					if(counter == target)
						return new GDposition(i,j);
					counter ++;
				}
			}
		}
	}
	if(target == "ally"){
		for(var i = 0 ; i<6 ; i++){
			for(var j = 0 ; j<5 ; j++){
				if(_board[i][j] != null && _GDNowChess.team == _board[i][j].team && Range(_GDNowChess.position,new GDposition(i,j),parseInt(range))){
					counter ++;
				}
			}
		}
		if(counter == 0)
			return null;
		var target = parseInt(counter * Math.random());
		counter = 0;
		for(var i = 0 ; i<6 ; i++){
			for(var j = 0 ; j<5 ; j++){
				if(_board[i][j] != null && _GDNowChess.team == _board[i][j].team && Range(_GDNowChess.position,new GDposition(i,j),parseInt(range))){
					if(counter == target)
						return new GDposition(i,j);
					counter ++;
				}
			}
		}
	}
	if(target == "enemy"){
		for(var i = 0 ; i<6 ; i++){
			for(var j = 0 ; j<5 ; j++){
				if(_board[i][j] != null && _GDNowChess.team != _board[i][j].team && Range(_GDNowChess.position,new GDposition(i,j),parseInt(range))){
					counter ++;
				}
			}
		}
		if(counter == 0)
			return null;
		var no = parseInt(counter * Math.random());
		counter = 0;
		for(var i = 0 ; i<6 ; i++){
			for(var j = 0 ; j<5 ; j++){
				if(_board[i][j] != null && _GDNowChess.team != _board[i][j].team && Range(_GDNowChess.position,new GDposition(i,j),parseInt(range))){
					if(counter == no)
						return new GDposition(i,j);
					counter ++;
				}
			}
		}
	}
	return null;
}